import Types from '../actionTypes';

import {getSelfBeforeLsit, getLogisticsTemplate, getSupplierOrderDetails, getAddress} from '../../api';

const SUPPLIER_ORDER_LIST = page => {
  return new Promise((resolve, reject) => {
    getSelfBeforeLsit(page).then(res => {
      console.log(res);
      resolve({
        type: Types.SUPPLIER_ORDER_LIST,
        res
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

const GET_SUPPLIER_ORDER_DETAILS = (order_no, supplier_id) => {
  return new Promise((resolve, reject) => {
    getSupplierOrderDetails(order_no, supplier_id).then(res => {
      resolve({
        type: Types.GET_SUPPLIER_ORDER_DETAILS,
        data: res.order[0]
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

const LOGISTICS_TEMPLATE = id => {
  return new Promise((resolve, reject) => {
    getLogisticsTemplate(id).then(res => {
      resolve({
        type: Types.LOGISTICS_TEMPLATE,
        data: res.data
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

const GET_ADDRESS = receipt_address_id => {
  return new Promise((resolve, reject) => {
    getAddress(receipt_address_id).then(res => {
      console.log(res);
      resolve({
        type: Types.GET_ADDRESS,
        data: res.data
      });
    }).catch(err => {
      console.log(err);
    });
  });
};

export default {
  SUPPLIER_ORDER_LIST,
  GET_SUPPLIER_ORDER_DETAILS,
  LOGISTICS_TEMPLATE,
  GET_ADDRESS
};
